import dash
from dash import html
import feffery_antd_components as fac
from dash.dependencies import Input, Output, State

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        fac.AntdSpace(
            [
                fac.AntdButton(
                    '按钮1',
                    id='button1',
                    type='primary'
                ),
                fac.AntdButton(
                    '按钮2',
                    id='button2',
                    type='primary'
                )
            ]
        ),
        fac.AntdDivider(isDashed=True),
        fac.AntdSpace(
            [
                fac.AntdText(
                    '初始化',
                    id=f'output-demo-{i}'
                )
                for i in range(10)
            ],
            direction='vertical'
        )
    ],
    style={
        'padding': '50px'
    }
)


def auto_fill_no_update(n, ignore_idx):
    return {
        i: dash.no_update
        for i in range(n)
        if i not in ignore_idx
    }


@app.callback(
    {
        i: Output(f'output-demo-{i}', 'children') for i in range(10)
    },
    [Input('button1', 'nClicks'),
     Input('button2', 'nClicks')]
)
def callback_demo(nClicks1, nClicks2):
    if any([nClicks1, nClicks2]):
        triggered_id = dash.callback_context.triggered[0]['prop_id']

        if triggered_id == 'button1.nClicks':
            output_dict = {
                i: f'按钮1被点击：{nClicks1}'
                for i in range(len(dash.callback_context.outputs_list))
                if i % 2 == 0
            }

            return {
                **output_dict,
                **auto_fill_no_update(len(dash.callback_context.outputs_list), output_dict.keys())
            }

        output_dict = {
            i: f'按钮2被点击：{nClicks2}'
            for i in range(len(dash.callback_context.outputs_list))
            if i % 2 == 1
        }

        return {
            **output_dict,
            **auto_fill_no_update(len(dash.callback_context.outputs_list), output_dict.keys())
        }

    return dash.no_update


if __name__ == '__main__':
    app.run_server(debug=True)
